import http from '@http';

const API_PREFIX = '/system/menu';

export function fetchGetMenuTree(values = {}) {
  return http.post(API_PREFIX + '/tree', values);
}

export function fetchGetMenu(id) {
  return http.get(API_PREFIX + '/detail/' + id);
}

export function fetchAddMenu(values) {
  const strategy = { CATALOG: 'catalog', MENU: 'menu', BUTTON: 'button' };
  const menuType = strategy[values.menuType?.toUpperCase()];
  if (!menuType) {
    return Promise.reject(new TypeError('菜单类型错误'));
  }
  delete values.menuType;
  return http.post(`${API_PREFIX}/insert/${menuType}`, values);
}

export function fetchUpdateMenu(values) {
  const strategy = { CATALOG: 'catalog', MENU: 'menu', BUTTON: 'button' };
  const menuType = strategy[values.menuType?.toUpperCase()];
  if (!menuType) {
    return Promise.reject(new TypeError('菜单类型错误'));
  }
  delete values.menuType;
  return http.post(`${API_PREFIX}/update/${menuType}`, values);
}

export function fetchDeleteMenu(ids) {
  return http.post(API_PREFIX + '/delete', ids);
}
